package com.zhaosc.shiro.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.zhaosc.shiro.dao.OrganizationDao;
import com.zhaosc.shiro.model.Organization;
import com.zhaosc.shiro.service.OrganizationService;

/**
 * 
 * @Description:TODO
 * @author:zhaosc
 * @time:2017年8月18日
 * @exception
 */
@Service
public class OrganizationServiceImpl implements OrganizationService {
//    @Autowired
//    private JdbcTemplate jdbcTemplate;
//	@PersistenceContext
//	private EntityManager entityManager;
    @Autowired
    private OrganizationDao organizationDao;

    @Override
    public Organization createOrganization(Organization organization) {
        return organizationDao.save(organization);
    }


    @Override
    public void deleteOrganization(Long organizationId) {
        organizationDao.delete(organizationId);
    }

    @Override
    public Organization findOne(Long organizationId) {
        return organizationDao.findOne(organizationId);
    }

    @Override
    public List<Organization> findAll() {
        return organizationDao.findAll();
    }

    @Override
    public List<Organization> findAllWithExclude(Organization exclude) {
        return organizationDao.findByIdNotAndParentIdsNotLike(exclude.getId(),exclude.getParentIds());
    }

	@Override
	public void updateOrganization(Organization organization) throws IllegalAccessException, InvocationTargetException {
		Organization old=organizationDao.findOne(organization.getId());
		BeanUtils.copyProperties(old, organization);
		organizationDao.save(old);
		
	}

//    @Override
//    public void move(Organization source, Organization target) throws IllegalAccessException, InvocationTargetException {
//    	source.setParentId(target.getId());
//    	source.setParentIds(target.getParentIds());
//    	this.updateOrganization(source);
//    	
//    	target
//    	this.updateOrganization(target);
//        String moveSourceDescendantsSql = "update sys_organization set parent_ids=concat(?, substring(parent_ids, length(?))) where parent_ids like ?";
//        jdbcTemplate.update(moveSourceDescendantsSql, target.makeSelfAsParentIds(), source.makeSelfAsParentIds(), source.makeSelfAsParentIds() + "%");
//    }
}
